<?php
	class UserIsLoggedInController
	{
		// Membervariables
		private $_userHandler = null;
		private $_registerHandler = null;
		private $_profileHandler = null;
		private $_profileView = null;
		private $_profileController = null;
		private $_messageHandler = null;
		private $_messageView = null;
		private $_messageController = null;
		private $_registerView = null;
		private $_userView = null;
				
		public function __construct(\UserHandler $userHandler, \RegisterHandler $registerHandler, \ProfileHandler $profileHandler, \ProfileView $profileView, \ProfileController $profileController, \MessageHandler $messHandler, \MessageView $messView, \MessageController $messageController, \RegisterView $registerView, \UserView $userView)
		{
			$this->_userHandler = $userHandler;
			$this->_registerHandler = $registerHandler;
			$this->_profileHandler = $profileHandler;
			$this->_profileView = $profileView;
			$this->_profileController = $profileController;
			$this->_messageHandler = $messHandler;
			$this->_messageView = $messView;
			$this->_messageController = $messageController;
			$this->_registerView = $registerView;
			$this->_userView = $userView;
		}
		
		public function DoControl()
		{
			$xhtml = "";
			// View
			$view = new UserIsLoggedInView();
			// Create a user object of the current user.
			$user = $this->_userHandler->GetUser($_SESSION[UserView::USERNAME]);
			
			// Retrieve the userType from the user object.
			$userType = $user->usertype;
			
			$xhtml .= "<div class ='MenuBar'>";
			
			// Check if the user has any new messages.
			if ($this->_messageHandler->DoesUserHaveNewMessages($user) == true)
			{
				// Count the number of new messages.
				$numberOfMessages = $this->_messageHandler->GetNumberOfNewMessages($user);
				$xhtml .= $view->DoMenu($userType, $numberOfMessages);
			}
			else 
			{
				$xhtml .= $view->DoMenu($userType, 0);
			}
			
			$xhtml .= "</div>";
			
			$xhtml .= "<div class ='Content'>";
			
			// Check if the user is admin.
			if ($userType == UserView::ADMIN) 
			{
				// Check if admin tried to administrate.
				if ($view->TriedToAdmin() == true)
				{
					$userList = $this->_userHandler->GetUserList();
				
					$xhtml .= $view->ListUsers($userList);
				}
				
				// Check wether the admin tried to delete a user.
				if ($view->TriedToDelete() == true)
				{
					// Delete the user
					$this->_userHandler->RemoveUserFromDB($view->GetDeleteId(), $view->GetDeleteUsername());
				
					// Then list the remaining users.
					$userList = $this->_userHandler->GetUserList();
					$xhtml .= $view->ListUsers($userList);
				}
				
				// Check wether the admin wants to add a new user.
				if($view->WantToAddUser() == true)
				{
					// Print the form
					$xhtml .= $this->_registerView->DoRegisterForm($userType);
				
					// Check wether he clicked the register button.
					if($this->_registerView->TriedToRegister() == true)
					{
						// Get the userinfo.
						$userInfo = $this->_registerView->GetUserInfo($userType);
					
						// Filter the info.
						$userInfo = $this->_registerHandler->FilterUserInput($userInfo);
					
						// Create a user object.
						$user = $this->_registerHandler->GenerateUserobjectFromUserinfo($userInfo);
					
						// Register the user.
						$this->_registerHandler->DoRegisterUser($user);
					}
				}
				
				// Used to make sure that the user is actually registered 
				// after the user clicked the register button.
				if ($this->_registerView->TriedToRegister() == true)
				{	
					$xhtml .= $this->_registerView->DoRegisterForm($userType);
				
					$userInfo = $this->_registerView->GetUserInfo($userType);
					
					$userInfo = $registerHandler->FilterUserInput($userInfo);
					
					$user = $registerHandler->GenerateUserobjectFromUserinfo($userInfo);
					
					$registerHandler->DoRegisterUser($user);
				}
				
				if ($view->ViewingErrors() == true)
				{
					$logView = new LogView();
					$xhtml .= $logView->ShowErrorLog();
				}
			}
			
			// Check wether the user treid to load his profile.
			if ($this->_profileView->TriedToLoadProfile() == true)
			{
				// Returns a user object of the current user.
				$user = $this->_userHandler->GetUser($_SESSION[UserView::USERNAME]);
				
				// Get the users profile
				$profile = $this->_profileHandler->GetProfile($user->id);
				$xhtml .= $this->_profileView->PrintProfile($profile, $user);
			}
			
			// Check wether the user tried to access their profile options.
			if ($this->_profileView->TriedToCheckOptions() == true)
			{
				$xhtml .= $this->_profileController->DoControl();
			}
			
			// Check wether the user tried to access the memberlist
			if ($view->ViewingMemberlist() == true)
			{
				// Get a memberlist
				$userlist = $this->_userHandler->GetUserList();
				// Print the list.
				$xhtml .= $this->_userView->PrintMemberlist($userlist);
			}
			
			// Check wether the user tried to load his profile.
			if ($this->_profileView->TriedToLoadMemberProfile() == true)
			{
				// Returns a user object of the current user.
				$user = $this->_userHandler->GetUser($_GET[ProfileView::MEMBER]);
				
				// Get the users profile
				$profile = $this->_profileHandler->GetProfile($user->id);
				$xhtml .= $this->_profileView->PrintProfile($profile, $user);
			}
			
			// Check wether the user tried to access his messages.
			if ($this->_messageView->TriedToLoadMessaging() == true)
			{
				$xhtml .= $this->_messageView->MessageMenu();
				
				// Get the user.
				$user = $this->_userHandler->GetUser($_SESSION[UserView::USERNAME]);
				
				// Get a list of the messages.
				$messageList = $this->_messageHandler->GetMemberMessageList($user);
				
				// Print all the messages.
				$xhtml .= $this->_messageView->PrintMessageList($messageList);
			}
			
			// Check if the user wants to read a specific message.
			if ($this->_messageView->TriedToReadMessage() == true)
			{
				$xhtml .= $this->_messageController->DoControl();
			}
			
			// Check if the user wants to write a new message.
			if ($this->_messageView->TriedToWriteNewMessage() == true)
			{
				$xhtml .= $this->_messageView->MessageMenu();
				$xhtml .= $this->_messageView->WriteNewMessageForm();
			}
			
			// Check if the user tried to send a new message.
			if ($this->_messageView->TriedToSendMessage() == true)
			{
				// Check if the message contained any errors.
				if ($this->_messageHandler->MessageDidNotCotainErrors() == true)
				{
					// Generate a message object.
					$message = $this->_messageHandler->GenerateMessage($user);
					
					$this->_messageHandler->SaveMessage($message);
				}
			}
			
			return $xhtml;
		}
	}
